package net.keebook.webapp.bean;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import net.keebook.webapp.dao.UserDao;
import net.keebook.webapp.util.MessageBundle;
import net.keebook.webapp.validator.UsernameValidator;

@ManagedBean
@RequestScoped
public class GenericValidationBean {
	@Inject
	private UserDao userDao;
	@Inject
	private UsernameValidator usernameValidator;
	
	public void validateUsername(FacesContext context, UIComponent component, Object value) {
		getUsernameValidator().validate(context, component, value);
	}
	
	public void validatePasswordChecked(FacesContext context, UIComponent component, Object value) {
		Boolean checked = (Boolean)value;
		if (checked == null || !checked) {
			((UIInput)component).setValid(false);
			context.addMessage(component.getClientId(context), new FacesMessage(MessageBundle.get().getString("error.verifyPassword")));
		}
	}
	
	public void validateTermsAcceptedChecked(FacesContext context, UIComponent component, Object value) {
		Boolean checked = (Boolean)value;
		if (checked == null || !checked) {
			((UIInput)component).setValid(false);
			context.addMessage(component.getClientId(context), new FacesMessage(MessageBundle.get().getString("error.acceptTerms")));
		}
	}
	
	public void deleteAccountChecked(FacesContext context, UIComponent component, Object value) {
		Boolean checked = (Boolean)value;
		if (checked == null || !checked) {
			((UIInput)component).setValid(false);
			context.addMessage(component.getClientId(context), new FacesMessage(MessageBundle.get().getString("error.confirmIntent")));
		}
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public UsernameValidator getUsernameValidator() {
		return usernameValidator;
	}

	public void setUsernameValidator(UsernameValidator usernameValidator) {
		this.usernameValidator = usernameValidator;
	}
}
